package mark.tom.friend.base.util;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SnackBarUtil {

    public static void showSnackBar(Context mContext, @StringRes int messageId) {
        if (mContext != null) {
            showSnackBar(mContext, mContext.getString(messageId));
        }
    }

    public static void showSnackBar(Context mContext, CharSequence message) {
        if (null == mContext || !(mContext instanceof Activity) || ((Activity) mContext).isFinishing()) {
            return;
        }
        View contentView = ((ViewGroup)(((Activity) mContext).findViewById(android.R.id.content))).getChildAt(0);
        Snackbar snackbar = setTextColorAndSize(shortMake(contentView, message), 0, 0, 10);
        snackbar.show();
    }

    public static Snackbar shortMake(View view, CharSequence message) {
        return Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
    }

    public static Snackbar shortMake(View view, int messageId) {
        return Snackbar.make(view, messageId, Snackbar.LENGTH_LONG);
    }

    public static Snackbar longMake(View view, CharSequence message) {
        return Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
    }

    public static Snackbar longMake(View view, int messageId) {
        return Snackbar.make(view, messageId, Snackbar.LENGTH_LONG);
    }

    public static Snackbar setSnackBarAlpha(Snackbar snackbar, float alpha) {
        Snackbar.SnackbarLayout snackBarView = getSnackBarLayout(snackbar);
        snackBarView.setAlpha(alpha);
        return snackbar;
    }

    public static Snackbar setSnackBarBackgroundColor(Snackbar snackbar, @ColorInt int color) {
        Snackbar.SnackbarLayout snackBarView = getSnackBarLayout(snackbar);
        snackBarView.setBackgroundColor(color);
        return snackbar;
    }

    public static Snackbar setSnackBarBackgroundResource(Snackbar snackbar, @DrawableRes int resId) {
        Snackbar.SnackbarLayout snackBarView = getSnackBarLayout(snackbar);
        snackBarView.setBackgroundResource(resId);
        return snackbar;
    }

    public static Snackbar setTextColorAndSize(Snackbar snackbar, @ColorInt int color, @Dimension float size, int maxLines) {
        Snackbar.SnackbarLayout snackBarView = getSnackBarLayout(snackbar);
        TextView snackBarTextView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
        if (color != 0) {
            snackBarTextView.setTextColor(color);
        }
        if (size != 0) {
            snackBarTextView.setTextSize(size);
        }
        if (maxLines > 0) {
            snackBarTextView.setMaxLines(maxLines);
        }
        return snackbar;
    }

    private static Snackbar.SnackbarLayout getSnackBarLayout(Snackbar snackbar) {
        return (Snackbar.SnackbarLayout) snackbar.getView();
    }
}
